// var i;
// var $eventManager = require("./EventManager");
// var $resKeeper = require("./ResKeeper");
// var $commonUtils = require("./CommonUtils");
// var $evenType = require("./EvenType");
// var $excel = require("./Excel");
// var $buffMag = require("./BuffMag");
// var $uI_SkillItem = require("./UI_SkillItem");
// var f = cc._decorator;
// var g = f.ccclass;
// var _ = f.property;
// var y = f.inspector;
// var m = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t.tipView = null;
//         t.rolelevel = null;
//         t.progress = null;
//         t.nowscallvView = null;
//         t.nowshoId = -1;
//         return t;
//     }
//     __extends(t, e);
//     t.prototype.onEnable = function () {
//         this.tipView.active = !1;
//         this.nowscallvView = this.node.getChildByName("New ScrollView").getComponent(cc.ScrollView);
//         this.onAddListener();
//         $eventManager.EventMgr.addEventListener($evenType.EVENT_TYPE.Game_Hide, this.closeTipView, this);
//         $eventManager.EventMgr.addEventListener($evenType.EVENT_TYPE.Game_Add_CiTiao, this.onAddListener, this);
//     };
//     t.prototype.closeTipView = function () {
//         this.nowshoId = -1;
//         this.tipView.active = !1;
//     };
//     t.prototype.onAddListener = function () {
//         var e = this;
//         var t = $buffMag.BuffMgr.ciTiaoData;
//         var o = {};
//         for (var n = 0; n < t.length; n++) {
//             var i = t[n];
//             var a = o[i.id];
//             if (!a) {
//                 a = {
//                     id: i.id
//                 };
//             }
//             var r = a.count;
//             if (!r) {
//                 r = 0;
//             }
//             r += 1;
//             a.count = r;
//             o[i.id] = a;
//         }
//         var c = Object.keys(o);
//         var s = this.nowscallvView.content.children;
//         s.forEach(function (e) {
//             e.active = !1;
//         });
//         for (n = 0; n < c.length; n++) {
//             var l = o[c[n]];
//             var u = s[n];
//             if (!u) {
//                 u = cc.instantiate(s[0]);
//                 this.nowscallvView.content.addChild(u);
//             }
//             u.active = !0;
//             u.getComponent($uI_SkillItem.default).initView(l, this.onClickItem.bind(this));
//         }
//         this.scheduleOnce(function () {
//             e.nowscallvView.scrollToBottomRight(0.2);
//         }, 0.5);
//         this.tipView.active = !1;
//         this.nowshoId = -1;
//     };
//     t.prototype.onClickItem = function (e) {
//         if (e.nowdata.id != this.nowshoId) {
//             var t = e.node.convertToWorldSpaceAR(cc.v2());
//             t = this.node.parent.convertToNodeSpaceAR(t);
//             this.tipView.x = t.x;
//             this.tipView.y = t.y + 200;
//             var o = this.node.parent.width - 380;
//             if (this.tipView.x + this.tipView.width / 2 > o / 2) {
//                 this.tipView.x = o / 2 - (this.tipView.width / 2 + 20);
//             }
//             if (this.tipView.x - this.tipView.width / 2 < -o / 2) {
//                 this.tipView.x = -(o / 2 - (this.tipView.width / 2 + 20));
//             }
//             this.tipView.getChildByName("jiantou").x = t.x - this.tipView.x;
//             var n = $excel.Excel.shuiguocitiao(e.nowdata.id);
//             var i = this.tipView.getChildByName("layout").getChildByName("lb").getComponent(cc.RichText);
//             var a = this.tipView
//                 .getChildByName("layout")
//                 .getChildByName("layout")
//                 .getChildByName("count")
//                 .getComponent(cc.Label);
//             var r = $excel.Excel.shuiguobuff(n.citiaoshuzhitype);
//             if (-1 != n.citiaochuxian) {
//                 this.tipView.getChildByName("layout").getChildByName("layout").active = !0;
//                 a.node.active = !0;
//                 a.string = e.nowdata.count * r.value[0] + "%";
//             } else {
//                 this.tipView.getChildByName("layout").getChildByName("layout").active = !1;
//             }
//             this.setCiTiaoContent(i, n, e.nowdata.count);
//             this.tipView.active = !0;
//             this.nowshoId = e.nowdata.id;
//         } else {
//             this.nowshoId = -1;
//             this.tipView.active = !1;
//         }
//     };
//     t.prototype.setCiTiaoContent = function (e, t) {
//         var o = t.citiaomiaoshu;
//         var n = $excel.Excel.shuiguobuff(t.citiaoshuzhitype);
//         var i = (o = $commonUtils.default.replaceSymbols(o, "n", [""])).split("X");
//         var a = "";
//         for (var r = 0; r < i.length; r++) {
//             var c = i[r];
//             if (n.value[r] && r < i.length - 1) {
//                 a += "<color=#ffffff>" + c + "</c><color=#00ff00>" + n.value[r] + "</color>";
//             } else {
//                 a += "<color=#ffffff>" + c + "</c>";
//             }
//         }
//         a = "<outline color=#000000 width = 4>" + a + "</outline>";
//         e.string = a;
//     };
//     t.prototype.onDisable = function () {
//         $eventManager.EventMgr.rmEventListener($evenType.EVENT_TYPE.Game_Hide, this.closeTipView, this);
//         $eventManager.EventMgr.rmEventListener($evenType.EVENT_TYPE.Game_Add_CiTiao, this.onAddListener, this);
//     };
//     t.prototype.updateUi = function (e, t) {
//         this.rolelevel.string = "Lv." + e;
//         this.progress.progress = t;
//     };
//     __decorate([_(cc.Node)], t.prototype, "tipView", void 0);
//     __decorate([_(cc.Label)], t.prototype, "rolelevel", void 0);
//     __decorate([_(cc.ProgressBar)], t.prototype, "progress", void 0);
//     return __decorate([g], t);
// })($resKeeper.default);
// exports.default = m;


// import { EventMgrInstance } from "./EventManager";
// import ResKeeper from "./ResKeeper";
// import CommonUtils from "./CommonUtils";
// import { EVENT_TYPE } from "./EvenType";
// import { Excel } from "./Excel";
// import { BuffMgr } from "./BuffMag";
// import UI_SkillItem from "./UI_SkillItem";

// const { ccclass, property, inspector } = cc._decorator;

// /**
//  * 建议类名：SkillManager
//  */
// @ccclass
// class SkillManager extends ResKeeper {
//     @property(cc.Node)
//     public tipView: cc.Node = null;

//     @property(cc.Label)
//     public rolelevel: cc.Label = null;

//     @property(cc.ProgressBar)
//     public progress: cc.ProgressBar = null;

//     private nowscallvView: cc.ScrollView = null;
//     private nowshoId: number = -1;

//     /**
//      * 当组件启用时调用
//      * 建议方法名：handleEnable
//      */
//     onEnable(): void {
//         this.tipView.active = false;
//         this.nowscallvView = this.node.getChildByName("New ScrollView").getComponent(cc.ScrollView);
//         this.onAddListener();
//         EventMgrInstance.addEventListener(EVENT_TYPE.Game_Hide, this.closeTipView, this);
//         EventMgrInstance.addEventListener(EVENT_TYPE.Game_Add_CiTiao, this.onAddListener, this);
//     }

//     /**
//      * 关闭提示视图
//      * 建议方法名：hideTipView
//      */
//     closeTipView(): void {
//         this.nowshoId = -1;
//         this.tipView.active = false;
//     }

//     /**
//      * 添加监听器
//      * 建议方法名：addListeners
//      */
//     onAddListener(): void {
//         const ciTiaoData = BuffMgr.ciTiaoData;
//         const ciTiaoMap: { [key: string]: { id: number, count?: number } } = {};

//         ciTiaoData.forEach(item => {
//             if (!ciTiaoMap[item.id]) {
//                 ciTiaoMap[item.id] = { id: item.id, count: 0 };
//             }
//             ciTiaoMap[item.id].count! += 1;
//         });

//         const ciTiaoKeys = Object.keys(ciTiaoMap);
//         const children = this.nowscallvView.content.children;

//         children.forEach(child => {
//             child.active = false;
//         });

//         ciTiaoKeys.forEach((key, index) => {
//             let child = children[index];
//             if (!child) {
//                 child = cc.instantiate(children[0]);
//                 this.nowscallvView.content.addChild(child);
//             }
//             child.active = true;
//             child.getComponent(UI_SkillItem).initView(ciTiaoMap[key], this.onClickItem.bind(this));
//         });

//         this.scheduleOnce(() => {
//             this.nowscallvView.scrollToBottomRight(0.2);
//         }, 0.5);

//         this.tipView.active = false;
//         this.nowshoId = -1;
//     }

//     /**
//      * 点击项目时的处理
//      * @param event 事件对象
//      * 建议方法名：handleItemClick
//      */
//     onClickItem(event: any): void {
//         if (event.nowdata.id !== this.nowshoId) {
//             let worldPos = event.node.convertToWorldSpaceAR(cc.v2());
//             let localPos = this.node.parent.convertToNodeSpaceAR(worldPos);
//             this.tipView.x = localPos.x;
//             this.tipView.y = localPos.y + 200;

//             const maxWidth = this.node.parent.width - 380;
//             if (this.tipView.x + this.tipView.width / 2 > maxWidth / 2) {
//                 this.tipView.x = maxWidth / 2 - (this.tipView.width / 2 + 20);
//             }
//             if (this.tipView.x - this.tipView.width / 2 < -maxWidth / 2) {
//                 this.tipView.x = -(maxWidth / 2 - (this.tipView.width / 2 + 20));
//             }

//             this.tipView.getChildByName("jiantou").x = localPos.x - this.tipView.x;

//             const ciTiaoData = Excel.shuiguocitiao(event.nowdata.id);
//             const richText = this.tipView.getChildByName("layout").getChildByName("lb").getComponent(cc.RichText);
//             const countLabel = this.tipView.getChildByName("layout").getChildByName("layout").getChildByName("count").getComponent(cc.Label);
//             const buffData = Excel.shuiguobuff(ciTiaoData.citiaoshuzhitype);

//             if (ciTiaoData.citiaochuxian !== -1) {
//                 this.tipView.getChildByName("layout").getChildByName("layout").active = true;
//                 countLabel.node.active = true;
//                 countLabel.string = `${event.nowdata.count * buffData.value[0]}%`;
//             } else {
//                 this.tipView.getChildByName("layout").getChildByName("layout").active = false;
//             }

//             this.setCiTiaoContent(richText, ciTiaoData, event.nowdata.count);
//             this.tipView.active = true;
//             this.nowshoId = event.nowdata.id;
//         } else {
//             this.nowshoId = -1;
//             this.tipView.active = false;
//         }
//     }

//     /**
//      * 设置词条内容
//      * @param richText 富文本组件
//      * @param ciTiaoData 词条数据
//      * @param count 词条数量
//      * 建议方法名：updateCiTiaoContent
//      */
//     setCiTiaoContent(richText: cc.RichText, ciTiaoData: any, count: number): void {
//         let description = ciTiaoData.citiaomiaoshu;
//         const buffData = Excel.shuiguobuff(ciTiaoData.citiaoshuzhitype);
//         description = CommonUtils.replaceSymbols(description, "n", [""]);
//         const parts = description.split("X");
//         let formattedText = "";

//         parts.forEach((part, index) => {
//             if (buffData.value[index] && index < parts.length - 1) {
//                 formattedText += `<color=#ffffff>${part}</c><color=#00ff00>${buffData.value[index]}</color>`;
//             } else {
//                 formattedText += `<color=#ffffff>${part}</c>`;
//             }
//         });

//         formattedText = `<outline color=#000000 width=4>${formattedText}</outline>`;
//         richText.string = formattedText;
//     }

//     /**
//      * 当组件禁用时调用
//      * 建议方法名：handleDisable
//      */
//     onDisable(): void {
//         EventMgrInstance.rmEventListener(EVENT_TYPE.Game_Hide, this.closeTipView, this);
//         EventMgrInstance.rmEventListener(EVENT_TYPE.Game_Add_CiTiao, this.onAddListener, this);
//     }

//     /**
//      * 更新UI
//      * @param level 角色等级
//      * @param progress 进度
//      * 建议方法名：refreshUI
//      */
//     updateUi(level: number, progress: number): void {
//         this.rolelevel.string = `Lv.${level}`;
//         this.progress.progress = progress;
//     }
// }

// export default SkillManager;

import { EventMgrInstance } from "./EventManager";
import ResKeeper from "./ResKeeper";
import CommonUtils from "./CommonUtils";
import { EVENT_TYPE } from "./EvenType";
import { Excel } from "./Excel";
import { BuffMgr } from "./BuffMag";
import ViewSkillItem from "./UI_SkillItem";

const { ccclass, property, inspector } = cc._decorator;

/**
 * 建议类名：SkillManager
 */
@ccclass
class ViewSkillShow extends ResKeeper {
    @property(cc.Node)
    public tipView: cc.Node = null; // 提示视图

    @property(cc.Label)
    public rolelevel: cc.Label = null; // 角色等级

    @property(cc.ProgressBar)
    public progress: cc.ProgressBar = null; // 进度条

    private scrollView: cc.ScrollView = null; // 滚动视图
    private selectedItemId: number = -1; // 当前选中的项目ID

    /**
     * 当组件启用时调用
     * 建议方法名：handleEnable
     */
    onEnable(): void {
        this.tipView.active = false;
        // 获取滚动视图组件
        this.scrollView = this.node.getChildByName("New ScrollView").getComponent(cc.ScrollView);
        this.addListeners();
        // 添加事件监听
        EventMgrInstance.addListener(EVENT_TYPE.Game_Hide, this.hideTipView, this);
        EventMgrInstance.addListener(EVENT_TYPE.Game_Add_CiTiao, this.addListeners, this);
    }

    /**
     * 关闭提示视图
     * 建议方法名：hideTipView
     */
    hideTipView(): void {
        this.selectedItemId = -1;
        this.tipView.active = false;
    }

    /**
     * 添加监听器
     * 建议方法名：addListeners
     */
    addListeners(): void {
        const ciTiaoData = BuffMgr.ciTiaoData;
        const ciTiaoMap: { [key: string]: { id: number, count?: number } } = {};

        // 构建词条数据映射
        ciTiaoData.forEach(item => {
            if (!ciTiaoMap[item.id]) {
                ciTiaoMap[item.id] = { id: item.id, count: 0 };
            }
            ciTiaoMap[item.id].count! += 1;
        });

        const ciTiaoKeys = Object.keys(ciTiaoMap);
        const children = this.scrollView.content.children;

        // 隐藏所有子节点
        children.forEach(child => {
            child.active = false;
        });

        // 更新显示词条子项
        ciTiaoKeys.forEach((key, index) => {
            let child = children[index];
            if (!child) {
                child = cc.instantiate(children[0]);
                this.scrollView.content.addChild(child);
            }
            child.active = true;
            child.getComponent(ViewSkillItem).initializeView(ciTiaoMap[key], this.handleItemClick.bind(this));
        });

        // 滚动到底部右侧
        this.scheduleOnce(() => {
            this.scrollView.scrollToBottomRight(0.2);
        }, 0.5);

        this.tipView.active = false;
        this.selectedItemId = -1;
    }

    /**
     * 点击项目时的处理
     * @param event 事件对象
     * 建议方法名：handleItemClick
     */
    handleItemClick(event: any): void {
        if (event.nowdata.id !== this.selectedItemId) {
            const worldPos = event.node.convertToWorldSpaceAR(cc.v2());
            const localPos = this.node.parent.convertToNodeSpaceAR(worldPos);
            this.tipView.x = localPos.x;
            this.tipView.y = localPos.y + 200;

            const maxWidth = this.node.parent.width - 380;
            if (this.tipView.x + this.tipView.width / 2 > maxWidth / 2) {
                this.tipView.x = maxWidth / 2 - (this.tipView.width / 2 + 20);
            }
            if (this.tipView.x - this.tipView.width / 2 < -maxWidth / 2) {
                this.tipView.x = -(maxWidth / 2 - (this.tipView.width / 2 + 20));
            }

            this.tipView.getChildByName("jiantou").x = localPos.x - this.tipView.x;

            const ciTiaoData = Excel.shuiguocitiao(event.nowdata.id);
            const richText = this.tipView.getChildByName("layout").getChildByName("lb").getComponent(cc.RichText);
            const countLabel = this.tipView.getChildByName("layout").getChildByName("layout").getChildByName("count").getComponent(cc.Label);
            const buffData = Excel.shuiguobuff(ciTiaoData.citiaoshuzhitype);

            if (ciTiaoData.citiaochuxian !== -1) {
                this.tipView.getChildByName("layout").getChildByName("layout").active = true;
                countLabel.node.active = true;
                countLabel.string = `${event.nowdata.count * buffData.value[0]}%`;
            } else {
                this.tipView.getChildByName("layout").getChildByName("layout").active = false;
            }

            this.updateCiTiaoContent(richText, ciTiaoData, event.nowdata.count);
            this.tipView.active = true;
            this.selectedItemId = event.nowdata.id;
        } else {
            this.selectedItemId = -1;
            this.tipView.active = false;
        }
    }

    /**
     * 设置词条内容
     * @param richText 富文本组件
     * @param ciTiaoData 词条数据
     * @param count 词条数量
     * 建议方法名：updateCiTiaoContent
     */
    updateCiTiaoContent(richText: cc.RichText, ciTiaoData: any, count: number): void {
        let description = ciTiaoData.citiaomiaoshu;
        const buffData = Excel.shuiguobuff(ciTiaoData.citiaoshuzhitype);
        description = CommonUtils.replaceStringSymbols(description, "n", [""]);
        const parts = description.split("X");
        let formattedText = "";

        parts.forEach((part, index) => {
            if (index < parts.length - 1 && buffData.value[index]) {
                formattedText += `<color=#ffffff>${part}</c><color=#00ff00>${buffData.value[index]}</color>`;
            } else {
                formattedText += `<color=#ffffff>${part}</c>`;
            }
        });

        richText.string = `<outline color=#000000 width=4>${formattedText}</outline>`;
    }

    /**
     * 当组件禁用时调用
     * 建议方法名：handleDisable
     */
    onDisable(): void {
        // 移除事件监听
        EventMgrInstance.removeListener(EVENT_TYPE.Game_Hide, this.hideTipView, this);
        EventMgrInstance.removeListener(EVENT_TYPE.Game_Add_CiTiao, this.addListeners, this);
    }

    /**
     * 更新UI
     * @param level 角色等级
     * @param progress 进度
     * 建议方法名：refreshUI
     */
    updateUI(level: number, progress: number): void {
        this.rolelevel.string = `Lv.${level}`;
        this.progress.progress = progress;
    }
}

export default ViewSkillShow;